package Aleehot100;

import C017.main;

/**
 * ClassName: c025
 * Package: Aleehot100
 * Description: 链表是否有环
 *
 * @Author BCXJ
 * @Create 2025/6/13 10:51
 * @Version 1.0
 * @Since 1.0
 */
public class c025 {
    public static void main(String[] args) {

    }
    static class ListNode {
        int val;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
            next = null;
        }
    }

    /**
     * 链表是否有环
     * @param head
     * @return
     */
    public static boolean hasCycle(ListNode head) {
        if(head == null) {
            return false;
        }
        ListNode slow = head, fast = head;
        while(fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
}
